<?php
namespace Common\Controller;

use Think\Controller;

class WeixinController extends Controller
{
    protected $config;

    public function _initialize()
    {
        $re = M('WechatConfig')->where("wid=%d", 1)->find();

        $config = array(

            'appid' => $re['appid'], //填写高级调用功能的app id

            'appsecret' => $re['appsecret'], //填写高级调用功能的密钥

            'wechat_id' => $re['wid']//微信id
        );

        $this->config = $config;

    }
    /**
     * curl请求
     * @access protected
     * @param string $url 请求地址
     * @param mix $data post参数
     * @return boole
     */
    private function https_request($url, $data = '')
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {

            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

            curl_setopt($curl, CURLOPT_HTTPHEADER, "Accept-Charset: utf-8");

            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');

            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($curl);

        if (curl_errno($curl)) {

            return 'ERROR ' . curl_error($curl);
        }

        curl_close($curl);

        return $result;
    }
}